﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Practices.Unity;

namespace TestProject
{
	public interface IStudent
	{
		string Name { get; set; }
		void ShowInfo();
	}

	public class QlinStudent
	{

		public string Name { get; set; }

		public IClass ToClass { get; set; }

		public QlinStudent(IClass _class)
		{
			ToClass = _class;
		}

		public void ShowInfo()
		{
			Console.WriteLine("{0}的班级：{1}", Name, ToClass.ClassName);
		}
	}

	/// <summary>
	/// 班级接口
	/// </summary>
	public interface IClass
	{
		string ClassName { get; set; }
		void ShowInfo();
	}

	/// <summary>
	/// 计科班
	/// </summary>
	public class CbClass : IClass
	{
		public string ClassName { get; set; }

		public void ShowInfo()
		{
			Console.WriteLine("计科班：{0}", ClassName);
		}
	}
	/// <summary>
	/// 电商班
	/// </summary>
	public class EcClass : IClass
	{
		public string ClassName { get; set; }

		public void ShowInfo()
		{
			Console.WriteLine("电商班：{0}", ClassName);
		}
	}

	public class Test1
	{
		public static void RegisterInstance()
		{
			IUnityContainer container = new UnityContainer();

			IClass myClass = new CbClass();

			IClass youClass = new EcClass();

			container.RegisterInstance<IClass>(myClass);

			container.RegisterInstance<IClass>("yourInstance", youClass);

			container.Resolve<IClass>().ShowInfo();

			container.Resolve<IClass>("yourInstance").ShowInfo();

		}

		public static void IuityTest()
		{
			IUnityContainer container = new UnityContainer();

			//默认注册（无命名）
			container.RegisterType<IClass, CbClass>();
			//命名注册
			container.RegisterType<IClass, EcClass>("ec");

			container.RegisterType(typeof(IClass), typeof(CbClass), "es");

			IClass cbClass = container.Resolve<IClass>();

			cbClass.ShowInfo();

			IClass ecClass = container.Resolve<IClass>("es");

			ecClass.ShowInfo();

			IEnumerable<IClass> classList = container.ResolveAll<IClass>();

			foreach (var item in classList)
			{
				item.ShowInfo();
			}
		}
	}

	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
	public class MyStringAttribute : Attribute
	{
		public MyStringAttribute(string des, int length)
		{
			MaxLength = length;
			this.Des = des;
		}
		public int MaxLength { get; set; }
		public int MinLength { get; set; }
		public string Des { get; set; }
	}
}
